﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.Linq;
using System.Text;
using Dataglot.Utils;

namespace Dataglot
{
    public class DbConfigUtils
    {
        private readonly NameValueCollection appSettings;
        private readonly ConnectionStringSettingsCollection connectionStrings;
        public DbConfigUtils()
        {
            appSettings = ConfigurationManager.AppSettings;
            connectionStrings = ConfigurationManager.ConnectionStrings;
        }

        public DbConfigUtils(NameValueCollection settings, ConnectionStringSettingsCollection connections)
        {
            appSettings = settings;
            connectionStrings = connections;
        }

        public string AppSettingIfExistsElseNull(string name)
        {
            return appSettings[name];
        }

        public  string ComposeNamespace(string sql)
        {
            return sql.Replace("{$ns}", Namespace());
        }

        public bool SplitParameterWordsWithUnderscore()
        {
            if (appSettings["Dataglot.SplitParameterWordsWithUnderscore"] == null)
            {
                return false;
            }
            return Convert.ToBoolean(appSettings["Dataglot.SplitParameterWordsWithUnderscore"]);
        }

        public  string Namespace()
        {
            if (appSettings["Dataglot.Namespace"] == null)
            {
                return "dbsettings";
            }
            return appSettings["Dataglot.Namespace"].Trim(new char[] { ' ', '_' });
        }


        private  void ValidationAppSettings(string name)
        {
            if (appSettings[name] == null)
            {
                throw new InvalidOperationException("Expected web.config or app.config to have a appSettings section with name of " +
                name + ", but didn't find it."
                );
            }
        }

        private  void ValidationConnectionString(string name)
        {
            if (connectionStrings[name] == null)
            {
                throw new InvalidOperationException("Expected web.config or app.config to have a connection string section with name of " +
                name + ", but didn't find it."
                );
            }
        }

        public string AccessConnectionString()
        {
            ValidationConnectionString("accessConnection");
            return connectionStrings["accessConnection"].ConnectionString;
        }

        public  string OleDbConnectionString()
        {
            ValidationConnectionString("OledbSql");
            return connectionStrings["OledbSql"].ConnectionString;    
        }

        public  string ConnectionString()
        {
            ValidationAppSettings("Dataglot.ConnectionStringName");
            string name = ConfigurationManager.AppSettings["Dataglot.ConnectionStringName"];
            ValidationConnectionString(name);
            return ConfigurationManager.ConnectionStrings[name].ConnectionString;
        }
        
        public  string Brand()
        {
            string how = appSettings["Dataglot.Brand"].ToLower();
            if (string.IsNullOrEmpty(how))
                throw new InvalidOperationException("Dataglot.Brand is missing from AppSettings");
            return how;
        }
    }
}
